package com.yidu.hbatsft.service.impl;
import com.github.pagehelper.PageHelper;
import com.yidu.hbatsft.dao.AtsEmpLeaderAndUserMapper;
import com.yidu.hbatsft.dao.AtsEmpLeaderMapper;
import com.yidu.hbatsft.domain.AtsEmpLeader;
import com.yidu.hbatsft.domain.AtsEmpLeaderVo;
import com.yidu.hbatsft.service.AtsEmpLeaderService;
import com.yidu.privilege.Dao.RoleDao;
import com.yidu.privilege.Dao.UserAndroleMapper;
import com.yidu.privilege.Dao.UserDao;
import com.yidu.privilege.Domain.Role;
import com.yidu.privilege.Domain.User;
import com.yidu.privilege.Domain.UserAndrole;
import com.yidu.utils.FileTools;
import com.yidu.utils.Page;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 领导业务层的实现类
 */
@Service
public class AatsEmpLeaderServiceimpl implements AtsEmpLeaderService {

    @Resource
    private AtsEmpLeaderMapper aelm;

    @Resource
    UserAndroleMapper userAndroleMapper;

    @Resource
    AtsEmpLeaderAndUserMapper atsEmpLeaderAndUserMapper;
    @Resource
    UserDao userDao;
    @Resource
    RoleDao roleDao;
    @Resource
    FileTools fileTools;
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public int addLeader(AtsEmpLeader ats,String time) {
        Date date = null;
        try {
           date = sdf.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        ats.setCreatTime(date);
        return aelm.insert(ats);
    }

    @Override
    public int updateLeader(AtsEmpLeader AtsEmpLeader) {
        return 0;
    }

    @Override
    public int deleteLeader(Integer id) {
        int cout = aelm.deleteByIdstate(id);
        if(cout >0){
            int c = userDao.deleteByLeaderId(id);
            if(c>0){
                return  cout;
            }
        }
        return  0;
    }


    @Override
    public List<AtsEmpLeader> findAll(Integer pageNum,Integer pageSize,String leaderName) {
        //开始的页数和行数
        PageHelper.startPage(pageNum,pageSize);

        return aelm.findAll(leaderName);
    }

    @Override
    public AtsEmpLeaderVo findById(Integer id) {

        return atsEmpLeaderAndUserMapper.findById(id);
    }

    @Override
    public int updateLeaderIsva(Integer id,Boolean value) {
        int state ;

        if(value){
            state = 1;
        }else{
            state = 0;
        }

        int count = aelm.updateByIdState(id,state);


        return count;
    }
    @Override
    public int updateLedaerData(AtsEmpLeaderVo vo,MultipartFile file) {
        //如果ID等于空那么新增，否则修改数据;
        AtsEmpLeader leader = new AtsEmpLeader();
        User user = new User();
        leader.setLeaderName(vo.getLeaderName());
        leader.setLeaderExp(vo.getLeaderExp());
        leader.setPhone(vo.getPhone());
        leader.setDeptName(vo.getDeptName());
        leader.setPostName(vo.getPostName());
        leader.setAddr(vo.getAddr());
        leader.setLeaderBusiness(vo.getLeaderBusiness());
        leader.setOther(vo.getRemark());
        if(vo.getRemark() == null){
            leader.setOther("暂无说明");
            leader.setRemark("暂无说明");
            user.setUsRemark("暂无说明");
        }
        leader.setOther(vo.getRemark());
        leader.setRemark(vo.getRemark());
        user.setUsName(vo.getUsName());
        user.setUsState("0");
        user.setUsType("领导");
        user.setUsRemark(vo.getRemark());
        //判断文件非空时
        if (file.getOriginalFilename() != null && !"".equals(file.getOriginalFilename())) {
            //处理异常
            try {
                String usPhoto = fileTools.saveFile("UserImg", file);
                user.setUsPhoto(usPhoto);//设置头像
                System.err.println("===================" + usPhoto);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        user.setUsPass(vo.getUsPass());
        user.setUsPhone(vo.getPhone());
        user.setUsEmail(vo.getUsEmail());
        UserAndrole userAndrole = new UserAndrole();
        if(vo.getLeaderId() == null){
            leader.setIsva(1);
            leader.setCreatTime(new Date());
            int cout = aelm.insert(leader);
            if(cout >0){
                user.setUsWaiid(leader.getLeaderId());
                int c = userDao.insert(user);
                userAndrole.setUsId(user.getUsId());
                //userAndrole.setRoId(leader.getLeaderId());
                Role role = roleDao.queryIdByLeaderName();
                userAndrole.setRoId(role.getRoId());
                //System.out.println("roID:"+leader.getLeaderId());
                if(c>0){

                    userAndroleMapper.insert(userAndrole);
                    System.out.println("新增完毕");
                    return cout;

                }
            }

        }else{
            //则是修改
            //System.out.println("进入修改！！！！======");
            System.err.println("usid:"+vo.getUsId()+"leaderId:"+vo.getLeaderId());
            leader.setLeaderId(vo.getLeaderId());
            user.setUsId(vo.getUsId());
            int t = aelm.updateByPrimaryKey(leader);
            if(t >0){
                System.out.println("进入了修改用户表的方法！！！！");
                userDao.update(user);
                return t;
            }
        }
        return 0;
    }

    @Override
    public int findCout() {
        return aelm.findCout();
    }

    @Override
    public List<AtsEmpLeader> findAllx() {
        return aelm.findAllx();
    }


}
